package com.example.springboot.controller;


import com.example.springboot.pojo.AdRequest;
import com.example.springboot.pojo.Result;
import com.example.springboot.service.AdRequestService;
import com.example.springboot.service.ValidationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.springboot.util.SendMail;


@RestController
@RequestMapping("/ad")
public class AdRequestController {

    @Autowired
    private AdRequestService adRequestService;

    @Autowired
    private ValidationService validationService;

    private AdRequest findById(Integer id){
        return adRequestService.findById(id);
    }

    @PostMapping("/addAdRequest")
    public Result<?> addAdRequest(String name,String email,String validation,String phoneNumber,String company,String adDetail){
        if(validationService.checkValidation(email,validation)){
            adRequestService.addAdRequest(name,email,phoneNumber,company,adDetail);
            return Result.success();
        }else{
            return Result.error("验证码错误");
        }
    }

    @GetMapping("/getAllAdRequest")
    public Result<?> getAllAdRequest(){
        AdRequest[] adRequestList =  adRequestService.getAllAdRequest();
        return Result.success(adRequestList);
    }

    @PutMapping("/deleteAdRequest")
    public Result<?> deleteAdRequest(Integer id){
        AdRequest ar = findById(id);
        if(ar == null){
            return Result.error("此id不存在");
        }else{
            adRequestService.deleteAdRequest(id);
            return Result.success();
        }
    }

    @PutMapping("/passAdRequest")
    public Result<?> passAdRequest(Integer id){
        AdRequest ar = findById(id);
        if(ar == null){
            return Result.error("此id不存在");
        }else{
            adRequestService.passAdRequest(id);
            SendMail.Mail_Code(ar.getEmail(),"【审核通知】","贵司的申请已被通过，请您择日前往平安物业洽谈。\n地址：江西省南昌市东湖区北京西路437号\n（周六周日及节假日无人值班）");
            return Result.success();
        }
    }

    @PutMapping("/refuseAdRequest")
    public Result<?> refuseAdRequest(Integer id){
        AdRequest ar = findById(id);
        if(ar == null){
            return Result.error("此id不存在");
        }else{
            adRequestService.refuseAdRequest(id);
            SendMail.Mail_Code(ar.getEmail(),"【审核通知】","很遗憾，贵司的申请已被否决。如有继续合作意愿，请重新提交您的商务合作申请。");
            return Result.success();
        }
    }
}
